﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;

public partial class System_Account_MenuList : BasePage
{
    sys_menuBLL bll = new sys_menuBLL();

    #region 属性
    private string StrWhere
    {
        get
        {
            return ViewState["StrWhere"] == null ? "" : ViewState["StrWhere"].ToString();
        }
        set
        {
            ViewState["StrWhere"] = value;
        }
    }
    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        base.CheckUserPermission("001002");
        if (!IsPostBack)
        {
            //设置分页控件文本
            setLanguage();

            //绑定下拉框
            BindDropDownList();

            //绑定数据
            BindRepeaterData("1=1");
        }
    }

    /// <summary>
    /// 设置分页控件文本
    /// </summary>
    private void setLanguage()
    {
        TextUtility.SetPaginateCtlText(PaginateCtl);
    }
    #endregion

    #region 数据绑定
    private void BindRepeaterData(string swhere)
    {
        int pageIndex = PaginateCtl.CurrentPageIndex;
        int pageSize = PaginateCtl.PageSize;
        UtilityBLL dbu = new UtilityBLL();

        //绑定数据源
        RepeaterList.DataSource = dbu.GetListByPage("view_menu_user", "*", swhere, "modularid,OrderNo", 0, pageIndex, pageSize);
        //返回记录条数
        PaginateCtl.RecordCount = dbu.GetRecordCount("view_menu_user", swhere);
        //设置记录总数的显示文本(此方法必须在设置PaginateCtl.RecordCount之后调用)
        TextUtility.SetPaginateCtlRecoText(PaginateCtl);
        RepeaterList.DataBind();
    }

    private void BindDropDownList()
    {
        List<sys_modular> list = new List<sys_modular>();
        sys_modularBLL bll = new sys_modularBLL();
        list = bll.GetModelList("Valid=1");
        this.drop_Modular.DataTextField = "ModularName";
        this.drop_Modular.DataValueField = "ModularId";
        this.drop_Modular.DataSource = list;
        this.drop_Modular.DataBind();

        this.drop_Modular.Items.Insert(0, new ListItem("--请选择--", "0"));
    }
    #endregion

    #region 查询
    /// <summary>
    /// 查询按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string swhere = "1=1";
        if (this.drop_Modular.SelectedIndex > 0)
        {
            swhere += " and ModularId=" + TextUtility.ReplaceText(this.drop_Modular.SelectedValue) + "";
        }
        if (this.txt_ModularName.Text.Trim().Length > 0)
        {
            swhere += " and MenuName like '%" + TextUtility.ReplaceText(this.txt_ModularName.Text.Trim()) + "%'";
        }
        if (this.txt_ModularCode.Text.Trim().Length > 0)
        {
            swhere += " and MenuCode like '%" + TextUtility.ReplaceText(this.txt_ModularCode.Text.Trim()) + "%'";
        }
        StrWhere = swhere;
        //分页控件回到第1页
        PaginateCtl.CurrentPageIndex = 1;
        BindRepeaterData(StrWhere);
    }
    #endregion

    #region 列表操作
    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label validText = (Label)e.Item.FindControl("ValidText");
            LinkButton Lbvalid = (LinkButton)e.Item.FindControl("BtnOperate");

            Lbvalid.Text = "修改";
            //设置启用禁用的显示文本
            if (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "Valid")) == 0)
            {
                validText.Text = "禁用";
                validText.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                validText.Text = "启用";
                validText.ForeColor = System.Drawing.Color.Green;
            }
        }
    }
    protected void RepeaterList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = int.Parse(e.CommandArgument.ToString());
        if (e.CommandName == "Valid")
        {
            sys_menu model;
            model = bll.GetModel(id);
            model.Valid = model.Valid == 0 ? 1 : 0;
            bll.Update(model);
        }
        else if (e.CommandName == "Delete")
        {
            bll.Delete(id);
        }
        BindRepeaterData(StrWhere);
    }
    #endregion

    #region 分页
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PaginateCtl_PageChanged(object sender, EventArgs e)
    {
        BindRepeaterData(StrWhere);
    }
    #endregion
}
